#!/usr/bin/python
# -*- coding: UTF-8 -*-

import requests
import urllib
import os
import stat


def downImg(src):
	f = src.split('/')[-1].split('?')[0]
	print(f)
	fpath = path + f
	if f.find('.') < 1:
		return
	if os.path.exists(fpath):
		return
	try:
		r = urllib.urlretrieve(src, path + f)
	except Exception, e:
		print(e)	
	finally:
		pass
	#r = urllib.urlretrieve(src, path + f)
	#print('download img:%s'%(src))



def procImg(img):
	#print(img)
	srcI = img.find('src="')
	srcE = img.find('"', srcI + 5)
	downImg(img[srcI+5: srcE])

def procHtml(s):
	#print(s)
	bodyI = s.find('<body')
	imgI = s.find('<img', bodyI)
	endI = 0
	while imgI > -1:
		endI = s.find('>', imgI)
		if imgI > -1 and endI > -1:
			imgstr = s[imgI:endI]
			procImg(imgstr)
		else:
			print('imgI:%d' %(imgI))
			print('endI:%d' %(endI))
		imgI = s.find('<img', endI)


global path
path = '/Volumes/data1/temp/img/'
os.chdir(path)
#更改权限 
os.chmod(path, stat.S_IRWXU|stat.S_IRWXG|stat.S_IRWXO)
pIdx = 1
while pIdx < 60:
	print('pageindex:%d'%(pIdx))
	u = 'http://news.cnblogs.com/page/{0:d}'.format(pIdx)
	r = requests.get(u)
	if r.status_code == 200:
		print(r.encoding)
		#print(r.content)
		procHtml(r.content)

	else:
		print('httpcode:%d'%(r.status_code))
	pIdx += 1
print('game over')